//
//  WMH_News_ViewController.m
//  wmh.ios.v5
//
//  Created by AndLi on 14-9-1.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WMH_NewsList_ViewController.h"
#import "EpointWebInfoListViewController.h"
#import "InfoListModel.h"
#import "EpointWebInfoListCell.h"
#import "MJRefresh.h"

#import "NewsCell.h"

#define StartIndex 0

@interface WMH_NewsList_ViewController ()
{
    NSMutableArray *dataArray;
    UITableView *tv;
    UISearchBar *sb;
    UISearchDisplayController *searchController;
    MJRefreshFooterView *_footer;
    MJRefreshHeaderView *_header;
    
    //列表索引
    int pageIndex;
    
    //每页列表数量
    int listPageCount;
    
    //搜索关键字
    NSString *keyWork;
    
    //字体大小
    double list_title_fontsize;
    double list_date_fontsize;
    //标题字数
    int list_title_length;
    
}

@end

@implementation WMH_NewsList_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    pageIndex       = StartIndex;
    listPageCount   = 20;
    
    list_title_fontsize = [[DBFrameService getConfigValue:WMH_InfoList_Title_FontSize]  floatValue];
    list_date_fontsize  = [[DBFrameService getConfigValue:WMH_InfoList_Date_FontSize]   floatValue];
    list_title_length   = [[DBFrameService getConfigValue:WMH_InfoList_Title_Length]    floatValue];
    
    dataArray = [[NSMutableArray alloc]init];
    
    tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, AppWidth, AppHeight-SystemTakeHeight)];
    tv.rowHeight = 75;
    tv.dataSource = self;
    tv.delegate = self;

    [self.view addSubview:tv];
    
    [self getData:pageIndex taskid:2];
    [self addHeader];
    [self addFooter];
}

//加载更多
- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = tv;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        pageIndex++;
        [self getData:pageIndex taskid:1];
    };
    _footer = footer;
}

//刷新
-(void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = tv;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        pageIndex = StartIndex;
        [self getData:pageIndex taskid:2];
    };
    _header = header;
}

-(void)getData:(int)_index taskid:(int)taskid
{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[NSString stringWithFormat:@"%i",_index]          forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",listPageCount]   forKey:@"PageSize"];
    [params setObject:CateNum_NEWS                                      forKey:@"CateNum"];
    [params setObject:@"0"                                              forKey:@"IsHeadNews"];
    [params setObject:@""                                               forKey:@"Title"];
    [self startTask:@"Task_GetInfoList" withParams:params withTaskId:taskid];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    [self hideMBPHUD];
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        [_footer endRefreshing];
        [_header endRefreshing];
        return;
    }
    
    //加载更多
    if (taskId==1) {
        
        NSMutableArray *listData = [arr objectAtIndex:2];
        
        for (InfoListModel *m in listData) {
            [dataArray addObject:m];
        }
        [tv reloadData];
        [searchController.searchResultsTableView reloadData];
        [_footer endRefreshing];
        if (listData.count<listPageCount&&keyWork.length==0) {
            [_footer removeFromSuperview];
        }
    }
    else if (taskId==2)
    {
        [_footer removeFromSuperview];
        [self addFooter];
        [dataArray removeAllObjects];
        NSMutableArray *listData = [arr objectAtIndex:2];
        
        for (InfoListModel *m in listData) {
            [dataArray addObject:m];
        }
        [_header endRefreshing];
        [tv reloadData];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *WebinfolistItem = @"WMH_NewsListCell";
//    WMH_NewsListCell *cell = (WMH_NewsListCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
//    
//    if(cell==nil){
//        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"WMH_NewsListCell"
//                                                    owner:self
//                                                  options:nil];
//        cell = [nib objectAtIndex:0];
//        
//    }
//    NSUInteger row = [indexPath row];
//    InfoListModel *info = [dataArray objectAtIndex:row];
//    
//    cell.infoTitleLabel.text = [NSString stringWithFormat:@"%@",info.Title];
//    //时间格式2013/2/20 9:00:00
//    NSString *infodatestr = info.InfoDate;
//    if([infodatestr rangeOfString:@"-"].location !=NSNotFound){
//        NSArray *str=[infodatestr componentsSeparatedByString:@" "];
//        NSArray *str2=[[str objectAtIndex:0] componentsSeparatedByString:@"-"];
//        cell.infoTimeLabel.text = [NSString stringWithFormat:@"%@-%@",[str2 objectAtIndex:1],[str2 objectAtIndex:2]];
//        
//    }else{
//        cell.infoTimeLabel.text = info.InfoDate;
//    }
//    
//    NSString *temptitle;
//    //控制标题长度
//    if(info.Title.length>list_title_length){
//        temptitle = [info.Title substringWithRange:NSMakeRange(0, list_title_length-1)];
//        temptitle = [NSString stringWithFormat:@"%@...",temptitle];
//    }else{
//        temptitle = info.Title;
//    }
//    cell.infoTitleLabel.text = temptitle;
//    
//    cell.infoTitleLabel.frame = CGRectMake(2,0, 320-2, 60);
//    cell.infoTimeLabel.frame =CGRectMake(320-91,30, 80, 18);
//    cell.imageView.frame =CGRectMake(320-45-31-20+8,30+2, 31, 15);
//    
//    cell.infoTimeLabel.font=[UIFont fontWithName:wmh_listitem_font size:list_date_fontsize];
//    cell.infoTitleLabel.font=[UIFont fontWithName:wmh_listitem_font size:list_title_fontsize];
//    
//    cell.infoTimeLabel.textColor = [UIColor grayColor];
//    
//    return cell;
    
    static NSString *cellid = @"NewsCell";
    NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"NewsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    InfoListModel *info = [dataArray objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.titleLabel.text = info.Title;
    cell.dateLabel.text = info.InfoDate;
    return cell;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60-2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    InfoListModel *m = [dataArray objectAtIndex:indexPath.row];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:m.InfoID      forKey:@"InfoID"];
    [params setObject:CateNum_NEWS  forKey:@"CateNum"];
    [params setObject:@"0"          forKey:@"IsNeedUrl"];
    WMH_NewsDetail_ViewController *view = [[WMH_NewsDetail_ViewController alloc]init];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}

@end

